﻿#include "Grammar_analysis.h"
#include<iostream>
#include<string>
#include<fstream>
#include<map>
using namespace std;
extern vector<vector<string> > dataFromTxt;
extern int i;
extern ofstream fout;
int j=0;
int m;
map<string, int> fun;
bool String() {
	string str1 = "STRCON";
	if (dataFromTxt[i][0] != str1) {
		return false;
	}
	fout << dataFromTxt[i][0] << " " << dataFromTxt[i][1] << endl;
	fout << "‎＜字符串＞" << endl;
	i++;
	return true;
}//字符串

bool ConstantDefiniation() {
	outdata();
	i++;
	if (dataFromTxt[i][0] != "INTTK" && dataFromTxt[i][0] != "CHARTK") return false;
	
	j = i;
	outdata();
	i++;
	if (dataFromTxt[i][0] != "IDENFR")  return false;

	while (dataFromTxt[i][0] == "IDENFR") {
		outdata();
		i++;
		if (dataFromTxt[i][0] != "ASSIGN") return false;
		outdata();
		i++;
		if (dataFromTxt[i][0] == "PLUS" || dataFromTxt[i][0] == "MINU") {
			outdata();
			i++;
		}
		if (!(dataFromTxt[i][0] == "INTCON" && dataFromTxt[j][0] == "INTTK") && !(dataFromTxt[i][0] == "CHARCON" && dataFromTxt[j][0] == "CHARTK")) {
			return false;
		}
		outdata();
		if (dataFromTxt[i][0] == "INTCON")
		{
			fout << "<整数>" << endl;
			cout << "<整数>" << endl;
		}
		i++;
		if (dataFromTxt[i][0] == "COMMA") {
			outdata();
			i++;
		}
		else if (dataFromTxt[i][0] == "SEMICN") {
			break;
		}
		else {
			return false;
			break;
		}
	}
	//结束本次常量定义循环
	cout << "<常量定义>" << endl;
	fout << "<常量定义>" << endl;
	outdata();
	i++;
	if (dataFromTxt[i][0] == "CONSTTK") {
		ConstantDefiniation();
	}
	else {
		// 分号后不再是常量定义循环，常量说明结束.
		fout << "<常量说明>" << endl;
		cout << "<常量说明>" << endl;
		return 1;
	}
	return true;
}//常量定义
bool VariableDeclaration() {
	fout << dataFromTxt[j][0] << " " << dataFromTxt[j][1] << endl;
	fout << dataFromTxt[j + 1][0] << " " << dataFromTxt[j + 1][1] << endl;
	cout << dataFromTxt[j][0] << " " << dataFromTxt[j][1] << endl;
	cout << dataFromTxt[j + 1][0] << " " << dataFromTxt[j + 1][1] << endl;
	while (dataFromTxt[i][0] == "SEMICN" || dataFromTxt[i][0] == "COMMA" || dataFromTxt[i][0] == "LBRACK") {
		if (dataFromTxt[i][0] == "SEMICN") {
			/*符号表插入还没写*/
			fout << "<变量定义>" << endl;
			cout << "<变量定义>" << endl;
			outdata();
			i++;
			return true;
		}
		else if (dataFromTxt[i][0] == "COMMA") {
			//插入符号表  缺！
			outdata();
			i++;
			if (dataFromTxt[i][0] == "IDENFR") {
				outdata();
				i++;
			}
			else {
				return false;
			}
		}
		else if (dataFromTxt[i][0] == "LBRACK") {
			/*符号表  缺*/
			outdata();
			i++;
			if (dataFromTxt[i][0] != "INTCON" || dataFromTxt[j][1] == "0") {
				return false;
			}
			outdata();
			i++;
			if (dataFromTxt[i][0] == "RBRACK") {
				outdata();
				i++;
			}
			else {
				return false;
			}
		}
	}
	return false;
}//变量定义
bool ReturnedFunctionDe() {
	fout << dataFromTxt[j][0] << " " << dataFromTxt[j][1] << endl;
	fout << dataFromTxt[j + 1][0] << " " << dataFromTxt[j + 1][1] << endl;
	fout << "<声明头部>" << endl;

	cout << dataFromTxt[j][0] << " " << dataFromTxt[j][1] << endl;
	cout << dataFromTxt[j + 1][0] << " " << dataFromTxt[j + 1][1] << endl;
	cout << "<声明头部>" << endl;
	/*填写符号表*/
	fun.insert(pair<string, int>(dataFromTxt[j + 1][1], 1));

	if (dataFromTxt[i][0] != "LPARENT") { return false; }
	outdata();
	i++;
	ParameterTable();
	if (dataFromTxt[i][0] != "RPARENT") return false;
	outdata();
	i++;
	if (dataFromTxt[i][0] != "LBRACE") return false;
	outdata();
	i++;
	ComplexSyntax();
	if (dataFromTxt[i][0] != "RBRACE") return false;
	outdata();
	i++;
	cout << "<有返回值函数定义>" << endl;
	fout << "<有返回值函数定义>" << endl;
	return true;
}//函数声明
bool UnReturnedFunctionDe() {
	if (dataFromTxt[i][0] != "IDENFR") return false;
	outdata();
	fun.insert(pair<string,int> (dataFromTxt[i][1],0));
	i++;
	if (dataFromTxt[i][0] != "LPARENT") return false;
	outdata();
	i++;
	ParameterTable();
	if (dataFromTxt[i][0] != "RPARENT") return false;
	else {
		outdata();
		i++;
	}
	if (dataFromTxt[i][0] != "LBRACE") return false;
	outdata();
	i++;
	ComplexSyntax();
	if (dataFromTxt[i][0] != "RBRACE") return false;
	outdata();
	i++;
	cout << "<无返回值函数定义>" << endl;
	fout << "<无返回值函数定义>" << endl;
	return true;
}//无返回值函数定义


bool ComplexSyntax() {
	bool isval = false;
	if (dataFromTxt[i][0] == "CONSTTK") {
		ConstantDefiniation();
	}
	while (dataFromTxt[i][0] == "INTTK" || dataFromTxt[i][0] == "CHARTK") {
		isval = true;
		j = i;
		i += 2;
		VariableDeclaration();
	}
	if (isval) {
		cout << "<变量说明>" << endl;
		fout << "<变量说明>" << endl;
	}
	SyntaxLie();
	cout << "<复合语句>" << endl;
	fout << "<复合语句>" << endl;
	return true;
}//复合语句
bool ParameterTable() {
	//缺！！

	while (dataFromTxt[i][0] == "INTTK" || dataFromTxt[i][0] == "CHARTK") {
		j = i;
		//缺
		outdata();
		i++;
		if (dataFromTxt[i][0] != "IDENFR") { return false; }

		//符号表插入
		outdata();
		i++;
		if (dataFromTxt[i][0] != "COMMA") {
			fout << "<参数表>" << endl;
			cout << "<参数表>" << endl;
			return true;
		}
		outdata();
		i++;
	}
	if (dataFromTxt[i][0] == "RPARENT") {
		fout << "<参数表>" << endl;
		cout << "<参数表>" << endl;
	}
	else {
		return false;
	}
	return true;
}//参数表
bool MainFunction() {
	outdata();
	i++;
	if (dataFromTxt[i][0] != "LPARENT") return false;
	else {
		outdata();
		i++;
	}
	if (dataFromTxt[i][0] != "RPARENT") return false;
	else {
		outdata();
		i++;
	}
	if (dataFromTxt[i][0] != "LBRACE") return false;
	else {
		outdata();
		i++;
	}
	/*缺！！！*/
	ComplexSyntax();
	if (dataFromTxt[i][0] != "RBRACE") return false;
	outdata();
	fout << "<主函数>" << endl;
	cout << "<主函数>" << endl;
	return true;
}//主函数
bool Expression() {
	if (dataFromTxt[i][0] == "PLUS" || dataFromTxt[i][0] == "MINU") {
		outdata();
		i++;
	}
	Item();
	while (dataFromTxt[i][0] == "PLUS" || dataFromTxt[i][0] == "MINU") {
		outdata();
		i++;
		Item();
	}
	cout << "<表达式>" << endl;
	fout << "<表达式>" << endl;
	return true;
}//表达式
bool Item() {
	Factor();
	while (dataFromTxt[i][0] == "MULT" || dataFromTxt[i][0] == "DIV") {
		outdata();
		i++;
		Factor();
	}
	cout << "<项>" << endl;
	fout << "<项>" << endl;
	return true;
}//项
bool Factor() {
	if (dataFromTxt[i][0] == "LPARENT") {
		//表达式
		outdata();
		i++;
		Expression();
		if (dataFromTxt[i][0] != "RPARENT") return false;
		outdata();
		i++;
	}
	else if (dataFromTxt[i][0] == "PLUS" || dataFromTxt[i][0] == "MINU") {
		outdata();
		i++;
		if (dataFromTxt[i][0] != "INTCON") { return false; }
		outdata();
		i++;
		fout << "<整数>" << endl;
		cout << "<整数>" << endl;
	}
	else if (dataFromTxt[i][0] == "INTCON") {
		outdata();
		i++;
		fout << "<整数>" << endl;
		cout << "<整数>" << endl;
	}
	else if (dataFromTxt[i][0] == "CHARCON") {
		outdata();
		i++;
	}
	else if (dataFromTxt[i][0] == "IDENFR") {
		j = i;
		outdata();
		i++;
		if (dataFromTxt[i][0] == "LPARENT") {
			//有返回值函数调用语句
			m = j;
			ReturnedFunction();
		}
		else if (dataFromTxt[i][0] == "LBRACK") {
			//数组 标识符
			outdata();
			i++;
			Expression();
			if (dataFromTxt[i][0] != "RBRACK") return false;
			outdata();
			i++;
		}
		else {
			//d单纯只是标识符
		}
	}
	else { return false; }
	fout << "<因子>" << endl;
	cout << "<因子>" << endl;
	return true;
}//因子
bool If() {
	Expression();
	string str = "LSS LEQ GRE GEQ EQL NEQ";
	string str2 = dataFromTxt[i][0];
	if (str2=="LSS"|| str2 == "LEQ" || str2 == "GRE" || str2 == "GEQ" || str2 == "EQL" || str2 == "NEQ" ) {
		outdata();
		i++;
		Expression();
	}
	cout << "<条件>" << endl;
	fout << "<条件>" << endl;
	return true;
}//条件
bool ReturnedFunction() {
	if (dataFromTxt[i][0] != "LPARENT") return false;
	outdata();
	i++;
	NumberTable();
	if (dataFromTxt[i][0] != "RPARENT") return false;
	outdata();
	i++;
	if (fun.find(dataFromTxt[m][1]) == fun.end()) 
		return false;
	else if (fun[dataFromTxt[m][1]] == 0) {
		cout << "<无返回值函数调用语句>" << endl;
		fout << "<无返回值函数调用语句>" << endl;
	}
	else {
		cout << "<有返回值函数调用语句>" << endl;
		fout << "<有返回值函数调用语句>" << endl;
	}
	return true;
}//有返回值函数调用语句
bool NumberTable() {
	if (dataFromTxt[i][0] != "RPARENT") {
		Expression();
		while (dataFromTxt[i][0] == "COMMA") {
			outdata();
			i++;
			Expression();
		}
	}
	if (dataFromTxt[i][0] != "RPARENT")return false;
	cout << "<值参数表>" << endl;
	fout << "<值参数表>" << endl;
	return true;
}//值参数表
bool SyntaxLie() {
	string str = "IFTK WHILETK DOTK FORTK LBRACE IDENFR SCANFTK PRINTFTK RETURNTK SEMICN";
	while (dataFromTxt[i][0]=="IFTK"|| dataFromTxt[i][0] == "WHILETK" || dataFromTxt[i][0] == "DOTK" || dataFromTxt[i][0] == "FORTK" || dataFromTxt[i][0] == "LBRACE" || dataFromTxt[i][0] == "IDENFR" || dataFromTxt[i][0] == "SCANFTK" || dataFromTxt[i][0] == "PRINTFTK" || dataFromTxt[i][0] == "RETURNTK" || dataFromTxt[i][0] == "SEMICN" ) {
		Syntax();
	}
	cout << "<语句列>" << endl;
	fout << "<语句列>" << endl;
	return true;
}//语句列

bool Syntax() {
	if (dataFromTxt[i][0] == "IFTK") {
		IfSyntax();
		fout << "<语句>" << endl;
		cout << "<语句>" << endl;
		return true;
	}
	else if (dataFromTxt[i][0] == "WHILETK" || dataFromTxt[i][0] == "DOTK" || dataFromTxt[i][0] == "FORTK") {
		Loop();
		cout << "<语句>" << endl;
		fout << "<语句>" << endl;
		return true;
	}
	else if (dataFromTxt[i][0] == "LBRACE") {
		outdata();
		i++;
		SyntaxLie();
		if (dataFromTxt[i][0] != "RBRACE") return false;
		outdata();
		i++;
		cout << "<语句>" << endl;
		fout << "<语句>" << endl;
		return true;
	}
	else if (dataFromTxt[i][0] == "IDENFR") {
		j = i;
		outdata();
		i++;
		if (dataFromTxt[i][0] == "LPARENT") {
			//缺
			//有/无返回值的函数调用语句
			m = j;
			ReturnedFunction();
		}
		else if (dataFromTxt[i][0] == "ASSIGN" || dataFromTxt[i][0] == "LBRACK") {
			AssignSyntax();
		}
		else return false;
	}
	else if (dataFromTxt[i][0] == "SCANFTK") {
		//读语句
		ReadSyntax();
	}
	else if (dataFromTxt[i][0] == "PRINTFTK") {
		//写语句
		WriteSyntax();
	
	}
	else if (dataFromTxt[i][0] == "RETURNTK") {
		//返回语句
		ReturnSyntax();
	}
	if (dataFromTxt[i][0] != "SEMICN") return false;
	else {
		outdata();
		i++;
	}
	cout << "<语句>" << endl;
	fout << "<语句>" << endl;
	return true;
}//语句

bool ReadSyntax() {
	outdata();
	i++;
	if (dataFromTxt[i][0] != "LPARENT") {
		return false;
	}
	outdata();
	i++;
	if (dataFromTxt[i][0] != "IDENFR")
		return false;
	outdata();
	i++;
	while (dataFromTxt[i][0] == "COMMA") {
		outdata();
		i++;
		if (dataFromTxt[i][0] != "IDENFR") {
			return false;
		}
		outdata();
		i++;
	}
	if (dataFromTxt[i][0] != "RPARENT") return false;
	else {
		outdata();
		i++;
	}
	cout << "<读语句>" << endl;
	fout << "<读语句>" << endl;
	return true;
}//读语句
bool WriteSyntax() {
	if (dataFromTxt[i][0] != "PRINTFTK") return false;
	outdata();
	i++;
	if (dataFromTxt[i][0] != "LPARENT") return false;
	outdata();
	i++;
	if (dataFromTxt[i][0] == "STRCON") {
		outdata();
		cout << "<字符串>" << endl;
		fout << "<字符串>" << endl;
		i++;
		if (dataFromTxt[i][0] == "COMMA") {
			outdata();
			i++;
			Expression();
		}
	}
	else {
		Expression();
	}
	if (dataFromTxt[i][0] != "RPARENT") return false;
	else {
		outdata();
		i++;
		cout << "<写语句>" << endl;
		fout << "<写语句>" << endl;
	}
	return true;
}//写语句
bool AssignSyntax() {
	if (dataFromTxt[i][0] == "ASSIGN") {
		outdata();
		i++;
		Expression();
	}
	else if (dataFromTxt[i][0] == "LBRACK") {
		outdata();
		i++;
		Expression();
		if (dataFromTxt[i][0] != "RBRACK") {
			return false;
		}
		else {
			outdata();
			i++;
		}
		if (dataFromTxt[i][0] != "ASSIGN") return false;
		outdata();
		i++;
		Expression();
	}
	else return false;
	cout << "<赋值语句>" << endl;
	fout << "<赋值语句>" << endl;
	return true;
}//赋值语句
bool IfSyntax() {
	//缺

	outdata();
	i++;
	if (dataFromTxt[i][0] != "LPARENT") return false;
	outdata();
	i++;
	If();

	if (dataFromTxt[i][0] != "RPARENT") return false;
	else {
		outdata();
		i++;
	}
	Syntax();
	if (dataFromTxt[i][0] == "ELSETK") {
		outdata();
		i++;
		Syntax();
	}
	cout << "<条件语句>" << endl;
	fout << "<条件语句>" << endl;
	return true;
}//条件语句
bool ReturnSyntax() {
	outdata();
	i++;
	if (dataFromTxt[i][0] == "LPARENT") {
		outdata();
		i++;
		Expression();
		if (dataFromTxt[i][0] != "RPARENT") return false;
		else {
			outdata();
			i++;
		}
	}
	else {

	}
	cout << "<返回语句>" << endl;
	fout << "<返回语句>" << endl;
	return true;
}//返回语句
bool Loop() {
	if (dataFromTxt[i][0] == "WHILETK") {
		outdata();
		i++;
		if (dataFromTxt[i][0] != "LPARENT") return false;
		outdata();
		i++;
		If();
		if (dataFromTxt[i][0] != "RPARENT") return false;
		else {
			outdata();
			i++;
		}
		Syntax();
	}
	else if (dataFromTxt[i][0] == "DOTK") {
		outdata();
		i++;
		Syntax();
		if (dataFromTxt[i][0] != "WHILETK") {
			return false;
		}
		else {
			outdata();
			i++;
		}
		if (dataFromTxt[i][0] != "LPARENT") return false;
		outdata();
		i++;
		If();
		if (dataFromTxt[i][0] != "RPARENT") return false;
		outdata();
		i++;
	}
	else if (dataFromTxt[i][0] == "FORTK") {
		outdata();
		i++;
		if (dataFromTxt[i][0] != "LPARENT") return false;
		outdata();
		i++;
		if (dataFromTxt[i][0] != "IDENFR") return false;
		outdata();
		i++;
		if (dataFromTxt[i][0] != "ASSIGN") return false;
		outdata();
		i++;
		Expression();
		if (dataFromTxt[i][0] != "SEMICN") return false;
		outdata();
		i++;
		If();
		if (dataFromTxt[i][0] != "SEMICN") return false;
		outdata();
		i++;
		if (dataFromTxt[i][0] != "IDENFR") return false;
		outdata();
		i++;
		if (dataFromTxt[i][0] != "ASSIGN") return false;
		outdata();
		i++;
		if (dataFromTxt[i][0] != "IDENFR") return false;
		outdata();
		i++;
		if (dataFromTxt[i][0] != "PLUS" && dataFromTxt[i][0] != "MINU") return false;
		outdata();
		i++;

		if (dataFromTxt[i][0] != "INTCON") return false;
		outdata();
		cout << "<步长>" << endl;
		fout << "<步长>" << endl;
		i++;
		if (dataFromTxt[i][0] != "RPARENT") return false;
		outdata();
		i++;
		Syntax();
	}
	else return false;
	cout << "<循环语句>" << endl;
	fout << "<循环语句>" << endl;
	return true;
}//循环语句
bool Program() {
	// 处理常量说明
	bool isval = false;
	if (dataFromTxt[i][0] == "CONSTTK") {
		ConstantDefiniation();
	}
	//处理变量说明和第一个有返回值函数定义
	if (dataFromTxt[i][0] == "INTTK" || dataFromTxt[i][0] == "CHARTK") {
		j = i;
		i++;
		if (dataFromTxt[i][0] == "IDENFR") {
			i++;
			//进入变量说明分支
			while (dataFromTxt[i][0] == "COMMA" || dataFromTxt[i][0] == "SEMICN" || dataFromTxt[i][0] == "LBRACK") {
				isval = true;
				VariableDeclaration();
				if (dataFromTxt[i][0] == "TNTTK" || dataFromTxt[i][0] == "CHARTK") {
					j = i;
					i++;
					if (dataFromTxt[i][0] == "IDENFR") {
						i++;
					}
					else {
						return false;
					}
				}
				else {
					//碰到void或错误，跳出，错误在最后处理
					if (isval) {
						fout << "<变量说明>" << endl;
						cout << "<变量说明>" << endl;
					}
					break;
				}
			}
			//对于正常结束的while循环，查看是否为有返回值函数定义
			if (dataFromTxt[i][0] == "LPARENT") {
				if (isval) {
					fout << "<变量说明>" << endl;
					cout << "<变量说明>" << endl;
				}
				//判断函数返回
				ReturnedFunctionDe();
			}
		}
		else return false;
		
	}
	//处理有、无返回值的函数定义和主函数
	while (dataFromTxt[i][0] == "VOIDTK" || dataFromTxt[i][0] == "INTTK" || dataFromTxt[i][0] == "CHARTK") {
		if (dataFromTxt[i][0] == "VOIDTK") {
			outdata();
			i++;
			if (dataFromTxt[i][0] == "MAINTK") {
				MainFunction();
				fout << "<程序>" << endl;
				cout << "<程序>" << endl;
				return true;
			}
			else if (dataFromTxt[i][0] == "IDENFR") {
				UnReturnedFunctionDe();
			}
			else return false;
		}
		else {
			j = i;
			i++;
			if (dataFromTxt[i][0] == "IDENFR") {
				i++;
				ReturnedFunctionDe();
			}

		}
	}
	return true;
}//程序

